﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace CRM.Infrastructure.Tools
{
    public class JWTUtil
    {

        /// <summary>
        /// 生成token
        /// </summary>
        /// <param name="claims"></param>
        /// <param name="secretKey"></param>
        /// <param name="issuer"></param>
        /// <returns></returns>
        public static string EnJwt(Claim[] claims,string secretKey, string issuer)
        {
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwt = new JwtSecurityToken(
                issuer: issuer,
                audience: issuer,
                claims: claims,
                expires: DateTime.Now.AddMinutes(60 * 24), //token 
                signingCredentials: creds);
            var token = new JwtSecurityTokenHandler().WriteToken(jwt);

            return token;
        }


    }
}
